package Activemq;

import java.util.Objects;

public class Node implements Comparable<Node> {
    public Node parent;
    public final int x;
    public final int y;
    public double g;  // 实际移动代价
    public double h;  // 启发式估计代价
    public double f;  // 总代价 (f = g + h)

    public Node(Node parent, int x, int y) {
        this(parent, x, y, 0, 0);
    }

    public Node(Node parent, int x, int y, double g, double h) {
        this.parent = parent;
        this.x = x;
        this.y = y;
        this.g = g;
        this.h = h;
        this.f = g + h;
    }

    @Override
    public String toString() {
        return "Node{" +
                "parent=" + (parent != null ? parent.getKey() : "null") +
                ", x=" + x +
                ", y=" + y +
                ", g=" + g +
                ", h=" + h +
                ", f=" + f +
                '}';
    }

    public String getKey() {
        return x + "," + y;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Node)) return false;
        Node node = (Node) o;
        return x == node.x && y == node.y;
    }

    @Override
    public int hashCode() {
        return Objects.hash(x, y);
    }

    @Override
    public int compareTo(Node other) {
        return Double.compare(this.f, other.f);
    }
}